package com.sale.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sale.goods.domain.*;
import com.sale.goods.domain.dto.SysDateChangeDto;
import com.sale.goods.mapper.*;
import com.sale.goods.service.GoodPublicSelectService;
import com.sale.storage.api.vo.UpdateDateChangeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 财务模块公共查询实现层（日期变更使用）
 */
@RequiredArgsConstructor
@Service
public class GoodPublicSelectServiceImpl implements GoodPublicSelectService {

    private final GoodsCollectionMapper goodsCollectionMapper;

    private final GoodsPaymentMapper goodsPaymentMapper;

    private final GoodsIncomeMapper goodsIncomeMapper;

    private final GoodsDisburseMapper goodsDisburseMapper;

    private final GoodsTurnMapper goodsTurnMapper;

    private final GoodsNuclearMapper goodsNuclearMapper;

    /**
     * 财务单据查询
     */
    @Override
    public SysDateChangeDto goodSelect(String documentNumber) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SysDateChangeDto sysDateChangeDto = new SysDateChangeDto();
        //收款单
        GoodsCollection goodsCollection = goodsCollectionMapper.selectOne(new QueryWrapper<GoodsCollection>()
            .lambda().eq(GoodsCollection::getCollectionCode, documentNumber));
        if (goodsCollection != null) {
            sysDateChangeDto.setDocumentType("收款单");
            String collectionTime = goodsCollection.getCollectionTime();
            if (collectionTime != null) {
                Date parse = dateFormat.parse(collectionTime);
                sysDateChangeDto.setOriginalBillingTime(parse);
            }
            return sysDateChangeDto;
        }
        // 付款单
        GoodsPayment goodsPayment = goodsPaymentMapper.selectOne(new QueryWrapper<GoodsPayment>()
            .lambda().eq(GoodsPayment::getPaymentCode, documentNumber));
        if (goodsPayment != null) {
            sysDateChangeDto.setDocumentType("付款单");
            sysDateChangeDto.setOriginalBillingTime(goodsPayment.getCreateTime());
            return sysDateChangeDto;
        }

        // 其他收入单
        GoodsIncome goodsIncome = goodsIncomeMapper.selectOne(new QueryWrapper<GoodsIncome>()
            .lambda().eq(GoodsIncome::getIncomeCode, documentNumber));
        if (goodsIncome != null) {
            sysDateChangeDto.setDocumentType("其他收入单");
            String incomeTime = goodsIncome.getIncomeTime();
            if (incomeTime != null) {
                Date parse = dateFormat.parse(incomeTime);
                sysDateChangeDto.setOriginalBillingTime(parse);
            }
            return sysDateChangeDto;
        }
        // 其他支出单
        GoodsDisburse goodsDisburse = goodsDisburseMapper.selectOne(new QueryWrapper<GoodsDisburse>()
            .lambda().eq(GoodsDisburse::getDisburseCode, documentNumber));
        if (goodsDisburse != null) {
            sysDateChangeDto.setDocumentType("其他支出单");
            String disburseTime = goodsDisburse.getDisburseTime();
            if (disburseTime != null) {
                Date parse = dateFormat.parse(disburseTime);
                sysDateChangeDto.setOriginalBillingTime(parse);
            }
            if (goodsDisburse.getExamineTime() != null) {
                sysDateChangeDto.setOriginalAuditTime(goodsDisburse.getExamineTime());
            }
            return sysDateChangeDto;
        }

        // 转账单
        GoodsTurn goodsTurn = goodsTurnMapper.selectOne(new QueryWrapper<GoodsTurn>()
            .lambda().eq(GoodsTurn::getTurnCode, documentNumber));
        if (goodsTurn != null) {
            sysDateChangeDto.setDocumentType("转账单");
            String turnTime = goodsTurn.getTurnTime();
            if (turnTime != null) {
                Date parse = dateFormat.parse(turnTime);
                sysDateChangeDto.setOriginalBillingTime(parse);
            }
            if (goodsTurn.getExamineTime() != null) {
                sysDateChangeDto.setOriginalAuditTime(goodsTurn.getExamineTime());
            }
            return sysDateChangeDto;
        }
        // 核销单
        GoodsNuclear goodsNuclear = goodsNuclearMapper.selectOne(new QueryWrapper<GoodsNuclear>()
            .lambda().eq(GoodsNuclear::getNuclearCode, documentNumber));
        if (goodsNuclear != null) {
            sysDateChangeDto.setDocumentType("核销单");
            String nuclearTime = goodsNuclear.getNuclearTime();
            if(nuclearTime != null){
                Date parse = dateFormat.parse(nuclearTime);
                sysDateChangeDto.setOriginalBillingTime(parse);
            }
            if (goodsNuclear.getExamineTime() != null) {
                sysDateChangeDto.setOriginalAuditTime(goodsNuclear.getExamineTime());
            }
            return sysDateChangeDto;
        }
        // 成本调整单

        return sysDateChangeDto;
    }

    /**
     * 财务单据修改
     *
     * @param updateDateChangeVo
     * @return
     */
    @Override
    public boolean goodUpdate(UpdateDateChangeVo updateDateChangeVo) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //收款单
        GoodsCollection goodsCollection = goodsCollectionMapper.selectOne(new QueryWrapper<GoodsCollection>()
            .lambda().eq(GoodsCollection::getCollectionCode, updateDateChangeVo.getDocumnetNumber()));
        if (goodsCollection != null) {
            Date changeBillingTime = updateDateChangeVo.getChangeBillingTime();
            String format = dateFormat.format(changeBillingTime);
            goodsCollection.setCollectionTime(format);
            goodsCollection.setExamineTime(updateDateChangeVo.getChangeAuditTime());
            return goodsCollectionMapper.updateById(goodsCollection) > 0;
        }
        // 付款单
        GoodsPayment goodsPayment = goodsPaymentMapper.selectOne(new QueryWrapper<GoodsPayment>()
            .lambda().eq(GoodsPayment::getPaymentCode, updateDateChangeVo.getDocumnetNumber()));
        if (goodsPayment != null) {
            goodsPayment.setCreateTime(updateDateChangeVo.getChangeBillingTime());
            goodsPayment.setExamineTime(updateDateChangeVo.getChangeAuditTime());
            return goodsPaymentMapper.updateById(goodsPayment) > 0;
        }

        // 其他收入单
        GoodsIncome goodsIncome = goodsIncomeMapper.selectOne(new QueryWrapper<GoodsIncome>()
            .lambda().eq(GoodsIncome::getIncomeCode, updateDateChangeVo.getDocumnetNumber()));
        if (goodsIncome != null) {
            Date changeBillingTime = updateDateChangeVo.getChangeBillingTime();
            String format = dateFormat.format(changeBillingTime);
            goodsIncome.setIncomeTime(format);
            goodsIncome.setExamineTime(updateDateChangeVo.getChangeAuditTime());
            return goodsIncomeMapper.updateById(goodsIncome) > 0;
        }
        // 其他支出单
        GoodsDisburse goodsDisburse = goodsDisburseMapper.selectOne(new QueryWrapper<GoodsDisburse>()
            .lambda().eq(GoodsDisburse::getDisburseCode, updateDateChangeVo.getDocumnetNumber()));
        if (goodsDisburse != null) {
            Date changeBillingTime = updateDateChangeVo.getChangeBillingTime();
            String format = dateFormat.format(changeBillingTime);
            goodsDisburse.setDisburseTime(format);
            goodsDisburse.setExamineTime(updateDateChangeVo.getChangeAuditTime());
            return goodsDisburseMapper.updateById(goodsDisburse) > 0;
        }

        // 转账单
        GoodsTurn goodsTurn = goodsTurnMapper.selectOne(new QueryWrapper<GoodsTurn>()
            .lambda().eq(GoodsTurn::getTurnCode, updateDateChangeVo.getDocumnetNumber()));
        if (goodsTurn != null) {
            Date changeBillingTime = updateDateChangeVo.getChangeBillingTime();
            String format = dateFormat.format(changeBillingTime);
            goodsTurn.setTurnTime(format);
            goodsTurn.setExamineTime(updateDateChangeVo.getChangeAuditTime());
            return goodsTurnMapper.updateById(goodsTurn) > 0;
        }
        // 核销单
        GoodsNuclear goodsNuclear = goodsNuclearMapper.selectOne(new QueryWrapper<GoodsNuclear>()
            .lambda().eq(GoodsNuclear::getNuclearCode, updateDateChangeVo.getDocumnetNumber()));
        if (goodsNuclear != null) {
            Date changeBillingTime = updateDateChangeVo.getChangeBillingTime();
            String format = dateFormat.format(changeBillingTime);
            goodsNuclear.setNuclearTime(format);
            goodsNuclear.setExamineTime(updateDateChangeVo.getChangeAuditTime());
            return goodsNuclearMapper.updateById(goodsNuclear) > 0;
        }
        // 成本调整单

        return false;
    }
}



